import Utils from './utils';
import Tracker from './tracker';
import JsError from './jsError';
import PromiseError from './promiseError';
import ResourcesError from './resourcesError';
import WhiteScreen from './whiteScreen';
import Exposure from './exposure';
import AutomaticBurialPoint from './automaticBurialPoint';
import LengthOfStay from './pageDwellTime';
import BehaviorRecorder from './behaviorRecorder';
import Pv from './pv';
import Perf from './perf';
import ResourcesPerf from './resourcesPerf';
import Xhr from './xhr';
import { MonitorSdkConfig, ReportParams } from './types';
import LongTask from './longTask';

let __TRACKER__: Tracker; // 上报实例
class RegisterMonitorSdk {
	forceObservers!: () => void;
	behaviorRecorder!: BehaviorRecorder;
	constructor(private monitorSdkConfig: MonitorSdkConfig) {
		Utils.log('VERSION 1.3.0');
		if (!monitorSdkConfig.pId) {
			Utils.log('warn', '请上传[pId]字段');
			return;
		}
		__TRACKER__ = new Tracker(monitorSdkConfig);
		if (monitorSdkConfig.jsErrorLog) {
			new JsError((params: ReportParams) => this.report(params));
		}
		if (monitorSdkConfig.promiseErrorLog) {
			new PromiseError((params: ReportParams) => this.report(params));
		}
		if (monitorSdkConfig.resourcesErrorLog) {
			new ResourcesError((params: ReportParams) => this.report(params));
		}
		if (monitorSdkConfig.blankScreenLog) {
			new WhiteScreen(
				(params: ReportParams) => this.report(params),
				monitorSdkConfig?.ignoreWhiteScreenElements
			);
		}
		if (monitorSdkConfig.exposureLog) {
			const exposure = new Exposure((params: ReportParams) =>
				this.report(params)
			);
			// 手动刷新曝光
			this.forceObservers = exposure.forceObservers.bind(exposure);
		}
		if (monitorSdkConfig.automaticBurialPointLog) {
			new AutomaticBurialPoint((params: ReportParams) => this.report(params));
		}
		if (monitorSdkConfig.pageDwellTimeLog) {
			new LengthOfStay((params: ReportParams) => this.report(params));
		}
		if (monitorSdkConfig.pvLog) {
			new Pv((params: ReportParams) => this.report(params));
		}
		if (monitorSdkConfig.xhrLog) {
			new Xhr((params: ReportParams) => this.report(params));
		}
		if (monitorSdkConfig.resourcesPerfLog) {
			new ResourcesPerf(
				(params: ReportParams) => this.report(params),
				monitorSdkConfig.reportUrl
			);
		}

		if (monitorSdkConfig.longTaskLog) {
			new LongTask((params: ReportParams) => this.report(params));
		}

		if (monitorSdkConfig.perfLog) {
			new Perf((params: ReportParams) => this.report(params));
		}

		if (monitorSdkConfig.behaviorRecorderLog) {
			this.behaviorRecorder = new BehaviorRecorder((params: ReportParams) =>
				this.report(params)
			);
		}
	}

	// 统一处理日志上报(入队列)
	report = (params: ReportParams) => {
		params.pId = this.monitorSdkConfig.pId;
		__TRACKER__.pushToQueue(params);
	};
}

export let monitorSdk: RegisterMonitorSdk;
export default function singletonMonitor(monitorSdkStrategy: MonitorSdkConfig) {
	return monitorSdk
		? monitorSdk
		: (monitorSdk = new RegisterMonitorSdk(monitorSdkStrategy));
}

export * from './types';
